
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  org.openbravo.pool;

import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Vector;

import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;

import control.RequestParameter;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

import pool.preprocess.Process;
import pool.tablename.Master_table;
import utility.CapitalChar;
import utility.ConvertToLower;

public class Pool 
{
	  String Preprocess;
      Hashtable<Object , Object> PropertyMap = new Hashtable<Object, Object>();
      String userName;
      String type;
      String process="add";
      String PROCESS="process";      
      String TYPE="type";
      Hashtable<Object, Object> Result = new Hashtable<Object, Object>();
      GetIdName gid;
	  public void setPreprocess(String Preprocess)
      {
    	  this.Preprocess =Preprocess;
      }
      
	  public void setPropertyMap(Hashtable<Object , Object> PropertyMap)
	  {
		  this.PropertyMap.putAll(PropertyMap);
	  }
	  
	  public void setData(String userName,String type)
	  {
		this.userName = userName;
		this.type = type;	
		gid= new GetIdName();
		PROCESS = gid.getId(PROCESS);
		TYPE = gid.getId(TYPE);
	  }
	  
	/*  public void setData(String type,String process)
	  {
		this.type = type;
		this.process = process;
		gid= new GetIdName();
		PROCESS = gid.getId(PROCESS);
		TYPE = gid.getId(TYPE);
	  }*/
	  
	  public void Process()
	  {
		
		  Process processboject;
		  String typeId =gid.getId(this.type);
		  Hashtable<Object, Object> Condition = new Hashtable<Object, Object>();
		  Condition.put("mid", typeId);
		  Condition.put("pid", PROCESS);
		  RecordSearch rs =new RecordSearch();
		  rs.setConditionMap(Condition);
		  String line[] = rs.getArrayFromResultSet();
		  Class C;
		  String PROCESSID=gid.getId(process);
		  for(int i=0;i<line.length;i++)
		  {
			  String splitline[] = line[i].split("\t");
			  Condition.put("mid", splitline[Column.pv_index-1]);
			  Condition.put("pid", PROCESSID);
			  rs.setConditionMap(Condition);
			  Condition.clear();
			  String result[] = rs.getArrayFromResultSet();
			  for(int j=0;j<result.length;j++)
			  { 
				  String splitresult[] = result[j].split("\t");
				  Condition.put("mid", splitresult[Column.pv_index-1]);
				  Condition.put("pid", TYPE);
				  rs.setConditionMap(Condition);
				  String temp[]= rs.getArrayFromResultSet();
				  for(int l=0;l<temp.length;l++)
				  {	  
					  String tempsplit[]= temp[l].split("\t");
					  Condition.clear();
					  String ClassName=gid.getItem(tempsplit[Column.pv_index-1]);
						try 
						{
							C = Class.forName("pool.preprocess."+CapitalChar.makeFirstCharCapital(ClassName));
							processboject = (Process)C.newInstance();							
							processboject.setType(type,"");
							processboject.setMasterMap(PropertyMap);
							processboject.setBasicMap(PropertyMap);
							processboject.setPropertyvector(new Vector<Object>());
							Class paramlist[] = {String.class};
							Object param[] = {splitresult[Column.pv_index-1]};
							String methodName = "before" + CapitalChar.makeFirstCharCapital("add"); 
							Method method = C.getMethod(methodName,paramlist);
							method.invoke(processboject, param);
							Result.putAll(processboject.gethashtable());
							System.out.print(Result);
						}
						catch (ClassNotFoundException e) 
						{
							e.printStackTrace();
						} 
						catch (InstantiationException e) 
						{	
							
						}
						catch (Exception e) 
						{
							e.printStackTrace();
						}
				  }	
			  }	
		  }	
		}  
		  
	  public static void main(String[] args) 
	  {
		Pool pool = new Pool();
		pool.setData("C_bank", "add");
		Hashtable<Object, Object> input =new Hashtable<Object, Object>();
		input.put("name", "1123");
		input.put("codebranch", "ssssssss");
		input.put("codebank", "ssssssss");
		input = ConvertToLower.convertHashKey(input);
		pool.setPropertyMap(input);
		pool.Process();
	  }
	  public Hashtable<Object, Object> getResult()
	  {
		  return Result;
	  }
}
